package com.seeyou.lake.ui

import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.app.ActivityOptionsCompat
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.seeyou.lake.R
import com.seeyou.lake.base.BaseFragment
import com.seeyou.lake.base.EventBusVo
import com.seeyou.lake.bean.ArticleBean
import com.seeyou.lake.bean.UserBean
import com.seeyou.lake.databinding.FragmentSecretBinding
import com.seeyou.lake.util.GlideUtil
import com.seeyou.lake.util.SPUtils
import com.seeyou.lake.viewmodel.MainViewModel
import com.seeyou.lake.widget.PasswordCheckDialog
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class SecretFragment : BaseFragment<FragmentSecretBinding, MainViewModel>() {

    private  var passwordCheckDialog: PasswordCheckDialog?=null
    private lateinit var userBean: UserBean
    override fun yourViewModel(): MainViewModel {
        return MainViewModel()
    }

    override fun yourViewBing(): FragmentSecretBinding {
        return FragmentSecretBinding.inflate(layoutInflater)
    }

    override fun init() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
         userBean = SPUtils.getInstance().getObject(SPUtils.SP_USER) as UserBean
        mViewBing.srlSecret.setColorSchemeColors(ContextCompat.getColor(requireContext(),R.color.badge_red))
        mViewBing.srlSecret.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener {
            mViewBing.srlSecret.isRefreshing = false
            mVModel.getArticleList(userBean.uid, 1)
        })
        val gridLayoutManager =object :
            GridLayoutManager(requireContext(),2, RecyclerView.VERTICAL,false){
            override fun canScrollHorizontally(): Boolean {
                return false
            }

            override fun canScrollVertically(): Boolean {
                return true
            }
        }

        val adapter1 = object : BaseQuickAdapter<ArticleBean, QuickViewHolder>() {
            override fun onCreateViewHolder(
                context: Context,
                parent: ViewGroup,
                viewType: Int
            ): QuickViewHolder {
                // 返回一个 ViewHolder
                return QuickViewHolder(R.layout.item_article, parent)
            }

            override fun onBindViewHolder(
                holder: QuickViewHolder,
                position: Int,
                item: ArticleBean?
            ) {
                val imgView = holder.getView<ImageView>(R.id.iv_item_article)
                GlideUtil.loadRoundImage(imgView.context,item?.url,8,imgView)
                holder.setText(R.id.tv_item_title,item?.title)

            }
        }
        mViewBing.rlSecret.layoutManager = gridLayoutManager
        mViewBing.rlSecret.adapter = adapter1
        adapter1. addOnItemChildClickListener(R.id.iv_item_article
        ) { adapter, view, position ->
            val intent = Intent(requireContext(),ArticleDetailActivityMy::class.java)
            intent.putExtra("articleBean", adapter.getItem(position)!!)
            val options = ActivityOptionsCompat.makeSceneTransitionAnimation(
                requireActivity(),
                view,
                "shareImage"
            )
            startActivity(intent, options.toBundle())
        }
       /* adapter1.setOnItemClickListener(object :BaseQuickAdapter.OnItemClickListener<ArticleBean>{
            override fun onClick(
                adapter: BaseQuickAdapter<ArticleBean, *>,
                view: View,
                position: Int
            ) {
                ArticleDetailActivityMy.open(requireActivity(),adapter.getItem(position)!!)
            }

        })*/

        mVModel.getArticleList(userBean.uid,0)

        mVModel.secretArticleResponse.observe(this){
            adapter1.submitList(it.data)
            if (it.data==null|| it.data!!.isEmpty()){
                mViewBing.rlSecret.visibility = View.GONE
                mViewBing.llEmpty2.visibility = View.VISIBLE
            }else{
                mViewBing.llEmpty2.visibility = View.GONE
                mViewBing.rlSecret.visibility = View.VISIBLE

            }
        }


         passwordCheckDialog = PasswordCheckDialog(requireContext()).setNativeButton {  }.setPositiveButton {
            val u = SPUtils.getInstance().getObject(SPUtils.SP_USER) as UserBean
            if (it ==u.secret ){
                mViewBing.flSecret.visibility = View.GONE
                mVModel.getArticleList(userBean.uid, 1)
                passwordCheckDialog?.dismiss()


            }else{
                mVModel.toast(getString(R.string.tip_password_1))
            }
        }

        mViewBing.flSecret.setOnClickListener {
            passwordCheckDialog?.show()
        }
    }

    companion object {
        @JvmStatic
        fun newInstance() =
            SecretFragment()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun finishEvent(event: EventBusVo?) {
        if (event == null) {
            return
        }

        if (event.type == 2) {
            mVModel.getArticleList(userBean.uid,1)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }
}